package com.wlan.dao.manage;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Hibernate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.Type;

import com.wlan.dao.common.AbstractTestCase;
import com.wlanauth.core.criteria.GeneralCriteria;
import com.wlanauth.core.criteria.GeneralCriteriaQuery;
import com.wlanauth.core.criteria.GeneralCriteriaResult;
import com.wlanauth.domain.manage.User;
import com.wlanauth.domain.radius.RadAcct;
import com.wlanauth.service.radius.RadAcctService;

public class RadAcctTestCase extends AbstractTestCase {

	public void testQuery(){
		GeneralCriteria criteria = new GeneralCriteria(RadAcct.class);
		criteria.add(Restrictions.eq("username", "zz"));
		//criteria.createAlias("", alias)
		DetachedCriteria criteria2 = DetachedCriteria.forClass(User.class);
		
		
		GeneralCriteriaQuery query = new GeneralCriteriaQuery();
		GeneralCriteriaResult<RadAcct> result = query.execute(criteria);
		
		System.out.println(result.getTotal());
	}
	public void testQueryByDateGroupAndDateStr(){
		
		String[] s = ("-%Y-%m"+"-%Y").split("\\%");
		
		ArrayList<String> format = new ArrayList<String>();
		for(String t:s){
			System.out.println(t);
			if(t!=null&&!t.equals("null")&&!format.contains(t)){
				format.add(t);
			}
		}
		Collections.sort(format, new Comparator<String>(){
			private int getLevel(String s){
				if(s=="%Y") return 1;
				if(s=="%m") return 2;
				if(s=="%d") return 3;
				if(s=="%H") return 4;
				return -1;
			}
			public int compare(String arg0, String arg1) {
				return getLevel(arg0)-getLevel(arg1);
			}
			
		});
		String formatStr = "";
		for(String t:format){
			formatStr+="-"+t;
		}
		System.out.println(formatStr.equals("")?"-----":formatStr.substring(1));
		
		RadAcctService service = (RadAcctService)this.getCtx().getBean("radius.RadAcctService");
		
		long start = System.currentTimeMillis();
		List<?> list = service.queryByDateGroupAndDateStr("%Y","%Y-%m", "2012-07");
		
		System.out.println("大小:"+list.size());
		
		for(Object obj:list){
			Object[] colVals = (Object[])obj;
			for(Object o:colVals){
				System.out.println(o);
			}
		}
		System.out.println("耗时:"+(System.currentTimeMillis()-start));
	}
	/**
	 * 统计当年月流量
	 */
	public void testQuerySumbyMonth(){
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		c.setTimeInMillis(0);
		c.set(Calendar.YEAR, year);
		/**
		 * 统计月流量（用户跨月时可能存在误差）
		 */
		GeneralCriteria criteria =  new GeneralCriteria(RadAcct.class);
		ProjectionList projections = Projections.projectionList();
		criteria.add(Restrictions.gt("acctStartTime", c.getTime()));
		projections.add(Projections.sqlGroupProjection("DATE_FORMAT(acctstarttime,'%Y-%m') as startdate", "startdate", new String[]{"startdate"}, new Type[]{Hibernate.STRING}));
		projections.add(Projections.sum("acctInputOctets"));
		projections.add(Projections.sum("acctOutputOctets"));
		criteria.setProjection(projections);
		
		GeneralCriteriaResult<Object[]> result = criteria.executeQuery();
		
		for(int i=0;i<result.getData().size();i++){
			Object[] obj = result.getData().get(i);
			System.out.println(obj[0]+":"+Long.valueOf(obj[1].toString())+":");
		}
		
	}
}
